import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.Date;

public class ServletCookie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies=request.getCookies();
        boolean tag=false;
        int len=0;
        int x=0;
        if(cookies!=null) len=cookies.length;
        for (int i = 0; i < len; i++) {
            if("lastAccessTime".equals(cookies[i].getName())){
                tag=true;
                x=i;
                System.out.println("lastAccessTime:"+cookies[i].getValue());
                break;
            }
        }
        if(tag){//找到了对应cookie信息
            String str=cookies[x].getValue();
            str=str.replace('|',' ');
            response.getWriter().write(str);
        }else {
            response.getWriter().write("not find lastAccessTime cookie");
        }
        Date nowTime=new Date();
        String nowTimeString=nowTime.toString();
        System.out.println(nowTimeString);
        nowTimeString=nowTimeString.replace(' ','|');
        Cookie cookie=new Cookie("lastAccessTime",nowTimeString);
        cookie.setMaxAge(60);
        response.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
